package Entities;

import java.util.ArrayList;

import Entities.LCAChain.sceneGraph;
import Shapes.Box2D;

import processing.core.PApplet;

public class Diet {
	
	ArrayList<LCAChain> foods = null;
	ArrayList<sceneGraph>  sgs = null;
	
	ArrayList<Box2D> back_boxes = null; // draw a background that, this back ground is used for compare and join, data modification
	
	PApplet app;
	
	public Diet(){}
	
	public void setFoods(ArrayList<LCAChain> f){
		this.foods = f;
	}
	
	public ArrayList<LCAChain> getFoods(){
		return this.foods;
	}
	
	//----------------generate the graphic interface  as well as interaction--------------------------//
	
	public ArrayList<Box2D> getBackBox(){
		return this.back_boxes;
	}
	
	public ArrayList<sceneGraph> getSceneGraph(){
		return this.sgs;
	}
	
	public void setupGraph(PApplet p){
		this.app = p;
	}
	
	public void genertateWholeView(){
		
		//delete the previous content
		if(sgs!=null){
			sgs.clear();	
			sgs = null;
		}
		
		if(back_boxes!=null){
			back_boxes.clear();
			back_boxes = null;
		}
	
		//create a new scene
		sgs = new ArrayList<sceneGraph>();
		back_boxes = new ArrayList<Box2D>();
		
		int num = foods.size();
		
		//to deploy the multi-scenes
		int intervalx = app.width/num;
		int temy = app.height;
		int scale = intervalx*3/5;
		int box_scale = intervalx/2;
		
		
		
		
		for(int i = 0; i <foods.size();i++)
		{
			sgs.add(foods.get(i).getShape(app, intervalx*i + intervalx/2, temy/2, scale));
			back_boxes.add(new Box2D(app));
			back_boxes.get(i).update(intervalx*i + intervalx/2, temy/2,scale);
		}
		
	}
	
    /**
     * @param i
     * @param p
     * @return  the single view which user selected
     */
	public sceneGraph getSingleView(int i, PApplet p){
		
		return foods.get(i).getShape(p, p.width/2,p.height/2,200);
	}

	public void addOneProcess(String name, int sectionX, int sectionY) {
		// TODO Auto-generated method stub
		
		//first select the right section
		int selected = -1;
				
		for(int i = 0; i<this.getFoods().size();i++)
		{
			if((sectionX>(this.getSceneGraph().get(i).posx - this.getSceneGraph().get(i).scale))&&(sectionX<(this.getSceneGraph().get(i).posx + this.getSceneGraph().get(i).scale))){
				if((sectionY>(this.getSceneGraph().get(i).posy - this.getSceneGraph().get(i).scale))&&(sectionY<(this.getSceneGraph().get(i).posy + this.getSceneGraph().get(i).scale))){
					selected = i;
				}

			}
		}
		
		if(selected == -1){
			return;
		}
		
		boolean ischanged = false;
	
		ischanged = this.getSceneGraph().get(selected).addOneProcess(name, sectionX, sectionY);
		
		if(ischanged){
			this.genertateWholeView();
		}
		
	}

}
